{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "def gcd(m,n): #必须有缩进\n",
    "    while n:\n",
    "        r = m%n\n",
    "        m = n\n",
    "        n = r\n",
    "    return m\n",
    "\n",
    "print(gcd(20,30))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "35\n"
     ]
    }
   ],
   "source": [
    "def area(width=2,height=3):\n",
    "    return width*height    \n",
    "print(area(4)) #第二个参数没有,用默认的3\n",
    "print(area(height=7,width=5)) #这样写就不用在意参数的顺序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "返回多个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2.3333333333333335, 1)\n"
     ]
    }
   ],
   "source": [
    "def multi(a, b):\n",
    "    return (a/b, a%b)\n",
    "print(multi(7,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "任意个参数,arg是列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "def f(*arg):\n",
    "    sum = 0\n",
    "    for i in arg:\n",
    "        sum += i\n",
    "    return sum\n",
    "\n",
    "print(f(1,2,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "任意个参数,arg是字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a : 1\n",
      "b : 2\n",
      "c : 3\n"
     ]
    }
   ],
   "source": [
    "def g(**arg):\n",
    "    for key in arg:\n",
    "        print(key,':',arg[key])  \n",
    "        \n",
    "g(a=1,b=2,c=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "空函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def h(a):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "全局变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "parameter = 1\n",
    "def glb(parameter):\n",
    "    return parameter + globals()['parameter']\n",
    "print(glb(2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "嵌套函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "def multiplier(factor):\n",
    "    def multiplyByFactor(number):\n",
    "        return number*factor\n",
    "    return multiplyByFactor\n",
    "\n",
    "double = multiplier(2)\n",
    "print(double(5))\n",
    "print(multiplier(2)(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
